package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.excel.RenewalRecnTemplate;
import com.yxinsur.imc.business.excel.RenewalRecnTemplateExport;
import com.yxinsur.imc.business.form.RecnExpenseSearchForm;
import com.yxinsur.imc.business.form.RecnSearchForm;
import com.yxinsur.imc.business.service.IImcRenewalRecnService;
import com.yxinsur.imc.business.vo.RenewalRecnDetailsCheckVo;
import com.yxinsur.imc.business.vo.RenewalRecnVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 续年手续费对账单（新版） Controller
 *
 * @author imc
 * @date 2021-05-25
 */
@Api(value = "续年手续费模块接口服务", tags = {"手续年续费模块接口服务"})
@RestController
@RequestMapping("/business/renewal/recn/")
public class ImcRenewalRecnController extends BaseController {
    @Autowired
    private IImcRenewalRecnService imcRenewalRecnService;

    /**
     * 根据主表ID查询协议费用科目
     *
     * @param recnId 主表ID
     * @return TableDataInfo
     */
    @ApiOperation("根据主表ID查询协议费用科目")
    @GetMapping("/expense/list/{recnId}")
    public TableDataInfo listContractExpense(@PathVariable("recnId") Long recnId) {
        startPage();
        return getDataTable(this.imcRenewalRecnService.listContractExpenseByRecnId(recnId));
    }

    /**
     * 生成续年手续费对账单
     *
     * @param searchForm RecnExpenseSearchForm
     * @return AjaxResult
     */
    @ApiOperation("生成续年手续费对账单")
    @Log(title = "生成续年手续费对账单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@RequestBody RecnExpenseSearchForm searchForm) {
        return this.imcRenewalRecnService.createRenewalRecn(searchForm.getSettleMonth(), searchForm.getSupplierId(), searchForm.getContractIdList(), searchForm.getContractExpenseIdList());
    }

    /**
     * 导入模板
     *
     * @return AjaxResult
     */
    @ApiOperation(value = "导入模板", produces = "application/octet-stream")
    @GetMapping("/export/template")
    public AjaxResult importTemplate() {
        ExcelUtil<RenewalRecnTemplateExport> util = new ExcelUtil<>(RenewalRecnTemplateExport.class);
        return util.importTemplateExcel("续年供应商手续费明细");
    }

    /**
     * 导入供应商数据并核对
     *
     * @param recnId 主表ID
     * @return AjaxResult
     */
    @ApiOperation(value = "导入供应商数据并核对")
    @PostMapping("/import/{recnId}")
    public AjaxResult importAndCheck(MultipartFile file, @PathVariable("recnId") Long recnId) throws Exception {
        return this.imcRenewalRecnService.importAndCheck(file, recnId);
    }

    /**
     * 查询主表列表
     */
    @ApiOperation("查询续年手续费对账单列表")
    @GetMapping("/list")
    public TableDataInfo list(RecnExpenseSearchForm searchForm) {
        startPage();
        List<RenewalRecnVo> list = imcRenewalRecnService.listRenewalRecnList(searchForm);
        return getDataTable(list);
    }

    /**
     * 查询核对结果列表
     *
     * @param searchForm RecnExpenseSearchForm
     * @return 结果
     */
    @ApiOperation("查询核对结果列表")
    @GetMapping("/check/list")
    public TableDataInfo checkImcRecnList(RecnExpenseSearchForm searchForm) {
        startPage();
        List<RenewalRecnDetailsCheckVo> renewalRecnDetailsCheckVoList = this.imcRenewalRecnService.listCheckResultList(searchForm);
        return getDataTable(renewalRecnDetailsCheckVoList);
    }

    /**
     * 导出核对结果
     *
     * @param searchForm RecnExpenseSearchForm
     * @return 结果
     */
    @ApiOperation("导出核对结果")
    @Log(title = "导出核对结果", businessType = BusinessType.EXPORT)
    @PostMapping("/check/export")
    public AjaxResult export(@RequestBody RecnExpenseSearchForm searchForm) throws NoSuchFieldException, IllegalAccessException {
        List<RenewalRecnTemplate> list = this.imcRenewalRecnService.export(searchForm);
        ExcelUtil<RenewalRecnTemplate> util = new ExcelUtil<>(RenewalRecnTemplate.class);
        return util.exportExcel(list, "续年手续费核对结果");
    }

    /**
     * 批量处理核对结果
     *
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("批量处理核对结果")
    @Log(title = "批量处理核对结果", businessType = BusinessType.UPDATE)
    @PostMapping("/check/dispose")
    public AjaxResult disposeImcRecn(@RequestBody RecnSearchForm recnSearchForm) {
        return this.imcRenewalRecnService.disposeImcRecn(recnSearchForm);
    }

    /**
     * 提交审核
     *
     * @param recnId 主表ID
     * @return 结果
     */
    @ApiOperation("提交审核")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/audit/{recnId}")
    public AjaxResult auditImcRecn(@PathVariable("recnId") Long recnId) {
        return this.imcRenewalRecnService.auditImcRecn(recnId);
    }

    /**
     * 审核：通过
     *
     * @param recnId         主表ID
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("审核通过")
    @Log(title = "审核通过", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/approved/{recnId}")
    public AjaxResult auditApproved(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return this.imcRenewalRecnService.auditApproved(recnId, recnSearchForm);
    }

    /**
     * 审核：退回
     *
     * @param recnId         主表ID
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("退回")
    @Log(title = "退回", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/return/{recnId}")
    public AjaxResult auditReturn(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return this.imcRenewalRecnService.auditReturn(recnId, recnSearchForm);
    }

    /**
     * 审核：反结
     *
     * @param recnId         主表ID
     * @param recnSearchForm RecnSearchForm
     * @return 结果
     */
    @ApiOperation("反结")
    @Log(title = "反结", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/revert/{recnId}")
    public AjaxResult auditRevert(@PathVariable("recnId") Long recnId, @RequestBody RecnSearchForm recnSearchForm) {
        return this.imcRenewalRecnService.auditRevert(recnId, recnSearchForm);
    }

    /**
     * 删除
     *
     * @param recnId 主表ID
     * @return AjaxResult
     */
    @ApiOperation("删除")
    @Log(title = "删除", businessType = BusinessType.UPDATE)
    @GetMapping("/delete/{recnId}")
    public AjaxResult delete(@PathVariable("recnId") Long recnId) {
        return this.imcRenewalRecnService.delete(recnId);
    }
}
